package cn.compose.online.util;

import cn.compose.common.util.StringUtils;
import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
public class ParamsParseUtils {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    private static String regex = ";|\n|;\n";

    public static String getDefStr(String field){
        try{
            String result="";
            List<String> list = new ArrayList<String>(Arrays.asList(field.split(regex)));
            if(CollectionUtil.isEmpty(list)){
                return "";
            }

            Iterator iterator = list.iterator();
            while (iterator.hasNext()){
                String item = iterator.next().toString();
                if(StringUtils.isEmpty(item)){
                    continue;
                }
                result += "    " + item+";\n";
            }
            return result;
        }catch (Exception e){
            log.error("getDefStr解析错误:",e);
            return field;
        }
    }


    public static List<String> getDef(String field){
        List<String> paramList = Lists.newArrayList();
        try{
            List<String> list = new ArrayList<String>(Arrays.asList(field.split(regex)));

            if(CollectionUtil.isEmpty(list)){
                return Collections.EMPTY_LIST;
            }

            for (String item : list) {
                if(StringUtils.isEmpty(item)){
                    continue;
                }
                String param = item.split(" ")[1].split("=")[0];
                paramList.add(param);
            }
        }catch (Exception e){
            log.error("getDef解析错误:",e);
            return Collections.EMPTY_LIST;
        }

        return paramList;
    }

    public static Map<String,String> getDefMap(String field){
        Map<String,String> paramValueMap = Maps.newLinkedHashMap();
        try{
            List<String> list = new ArrayList<String>(Arrays.asList(field.split(regex)));

            if(CollectionUtil.isEmpty(list)){
                return null;
            }

            for (String item : list) {
                if(StringUtils.isEmpty(item)){
                    continue;
                }
                String replace = item.replace(" ", "").replace(";", "").replace("def", "");
                String param = replace.split("=")[0];
                String paramValue = replace.split("=")[1];
                String objStr=paramValueMap.get(param);
                if(!StringUtils.isEmpty(objStr)){
                    return null;
                }
                paramValueMap.put(param,paramValue);
            }
        }catch (Exception e){
            log.error("getDefMap解析错误:",e);
            return null;
        }

        return paramValueMap;
    }




    public String transfer(){
        //条件为空

        //


        return null;
    }

    public static void main(String[] args) {
        String testStr = "def a=dfdsfsd;def b = dsfsdaf\ndef c= 123;";
//        String testStr = "def a=dfdsfsd";
        List<String> def = ParamsParseUtils.getDef(testStr);
        Map<String, String> defMap = ParamsParseUtils.getDefMap(testStr);
        System.out.println(Arrays.toString(def.toArray()));
        System.out.println(defMap.entrySet());
    }
}
